package com.walk.zworld.ui.fragment.video

import android.content.Intent
import android.view.View
import android.widget.CheckBox
import android.widget.RadioButton
import android.widget.RadioGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.walk.zworld.R
import com.walk.zworld.mvvm.Constant
import com.walk.zworld.ui.activity.LoginActivity
import com.walk.zworld.ui.fragment.video.model.DtDetailModel
import com.walk.zworld.ui.fragment.video.model.YouVideoViewModel
import com.walk.zworld.utils.GlobalUtil.getDimension
import com.base.lib.base.utils.MyMMKV.Companion.mmkv
import com.base.lib.base.view.BaseMvvmListFragment

class YouVideoFragment : BaseMvvmListFragment<DtDetailModel, YouVideoViewModel, YouVideoAdapter>() {

    companion object {
        fun newsInstance() = YouVideoFragment()
    }

    override fun onBindViewModel(): Class<YouVideoViewModel> = YouVideoViewModel::class.java
    override fun onBindAdapter(): Class<YouVideoAdapter> = YouVideoAdapter::class.java

    override fun initBundle() {
        params = mutableMapOf<String, Any>()
    }

    override fun initCommonView(view: View) {
        super.initCommonView(view)
        initTopView(R.layout.you_video_header_layout)
    }

    override fun initView(view: View) {
        mAdapter.bindSkeletonScreen(
            mRecyclerView,
            R.layout.skeleton_item_home_list,
            12
        )
        val gridSpaceItemDecoration = YouGridSpaceItemDecoration(getDimension(R.dimen.dp_5)).setNoShowSpace(0, 0)
        val staggeredGridLayoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
        staggeredGridLayoutManager.gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_NONE
        mRecyclerView.run {
            setHasFixedSize(true)
            setItemViewCacheSize(0)
            layoutManager = staggeredGridLayoutManager
            addItemDecoration(gridSpaceItemDecoration)

            addOnScrollListener(object: RecyclerView.OnScrollListener() {
                override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                    val first = IntArray(2)
                    staggeredGridLayoutManager.findFirstCompletelyVisibleItemPositions(first)
                    if (newState == RecyclerView.SCROLL_STATE_IDLE && (first[0] == 1 || first[1] == 1)) {
                        staggeredGridLayoutManager.invalidateSpanAssignments()
                    }
                }
            })
        }
        mAdapter.run {
            setOnItemClickListener { adapter, view, position ->
                if (data.size != 0) {
                    val data = data[position]
//                    WebViewActivity.start(activity, data.id, data.title, data.link)
                }
            }
            addChildClickViewIds(R.id.iv_like)
            setOnItemChildClickListener { adapter, view, position ->
                if (data.size == 0) return@setOnItemChildClickListener
                val res = data[position]
                when (view.id) {
                    R.id.iv_like -> {
                        if (!mmkv.decodeBool(Constant.IS_LOGIN, false)) {
                            startActivity(Intent(activity, LoginActivity::class.java))
                            return@setOnItemChildClickListener
                        }
                    }
                }
            }
        }
        val rg = findViewById<RadioGroup>(R.id.rg)
        val cbPic = findViewById<CheckBox>(R.id.cbPic)
        val cbVideo = findViewById<CheckBox>(R.id.cbVideo)
        rg.setOnCheckedChangeListener { _, _ -> initData() }
        cbVideo.setOnClickListener {initData()}
        cbPic.setOnClickListener {initData()}
    }

    override fun initData() {
        //判断排序
        val mutableMap = params as MutableMap<String, Any>
        val rbZongHe = findViewById<RadioButton>(R.id.rbZongHe)
        val rbHot = findViewById<RadioButton>(R.id.rbHot)
        val rbNew = findViewById<RadioButton>(R.id.rbNew)
        val cbPic = findViewById<CheckBox>(R.id.cbPic)
        val cbVideo = findViewById<CheckBox>(R.id.cbVideo)
        if (rbZongHe.isChecked) {
            mutableMap["sort"] = 0
        }
        if (rbHot.isChecked) {
            mutableMap["sort"] = 1
        }
        if (rbNew.isChecked) {
            mutableMap["sort"] = 2
        }
        //判断类型
        if (cbPic.isChecked) {
            mutableMap["type"] = 0
        }
        if (cbVideo.isChecked) {
            mutableMap["type"] = 1
        }
        if (!cbVideo.isChecked && !cbPic.isChecked) {
            mutableMap["type"] = 2
        }
        isRefresh = true
        onRefreshEvent()
    }

}